package app.modules.infrastructure.profile.application.web;

import app.modules.common.dto.web.WebIdDTO;
import app.modules.common.util.web.PageUtil;
import app.modules.infrastructure.profile.business.service.ProfileItemService;
import app.modules.infrastructure.profile.dto.web.WebProfileItemDTO;
import app.modules.infrastructure.profile.dto.web.WebProfileItemQueryDTO;
import app.modules.security.dto.web.role.WebRoleDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jasmine.framework.web.entity.WebResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author mh.z
 */
@RestController
public class ProfileItemController {
    private ProfileItemService profileItemService;

    public ProfileItemController(ProfileItemService profileItemService) {
        this.profileItemService = profileItemService;
    }

    @Operation(summary = "分页查询配置项")
    @PostMapping("/api/infrastructure/profile/pageProfileItemsByCond")
    public ResponseEntity<WebResult<List<WebProfileItemDTO>>> pageWebProfileItemDTOsByCond(
            @RequestBody WebProfileItemQueryDTO queryDTO) {
        Page page = PageUtil.getPage(queryDTO);

        List<WebProfileItemDTO> recordList = profileItemService.pageWebProfileItemDTOsByCond(queryDTO, page);
        WebResult result = PageUtil.getResult(recordList, page);

        return result.toOkEntity();
    }

    @Operation(summary = "查询指定 ID 的配置项")
    @PostMapping("/api/infrastructure/profile/getProfileItemById")
    public ResponseEntity<WebResult<WebProfileItemDTO>> getProfileItemById(
            @RequestBody WebIdDTO dto) {
        Long id = dto.getId();
        WebProfileItemDTO recordDTO = profileItemService.getWebProfileItemDtoById(id);

        return WebResult.success(recordDTO).toOkEntity();
    }

    @Operation(summary = "保存配置项")
    @PostMapping("/api/infrastructure/profile/saveProfileItem")
    public ResponseEntity<WebResult<WebProfileItemDTO>> saveProfileItem(
            @Valid @RequestBody WebProfileItemDTO saveDTO) {
        WebProfileItemDTO recordDTO = profileItemService.saveProfileItem(saveDTO);
        WebResult result = WebResult.success(recordDTO);

        return result.toOkEntity();
    }

    @Operation(summary = "更新配置项")
    @PostMapping("/api/infrastructure/profile/updateProfileItem")
    public ResponseEntity<WebResult<WebRoleDTO>> updateProfileItem(
            @Valid @RequestBody WebProfileItemDTO updateDTO) {
        WebProfileItemDTO recordDTO = profileItemService.updateProfileItem(updateDTO);
        WebResult result = WebResult.success(recordDTO);

        return result.toOkEntity();
    }

}
